import axiosIns, { axiosOdata } from '../utils/axios';
import { commentData } from '../utils/mock';

/**
|--------------------------------------------------
| Constants
|--------------------------------------------------
*/
const GET_COMMENTLIST = 'GET_COMMENTLIST';

/**
|--------------------------------------------------
| Actions
|--------------------------------------------------
*/
export function getCommentList({ pageNum }) {
  return async (dispatch, getState) => {
    const { pageSize } = getState().comment;
    const res = await axiosIns({ url: '/getCommentList' });
    const { data } = res;
    dispatch(getCommentListed(data));
  };
}

function getCommentListed(data) {
  return {
    type: GET_COMMENTLIST,
    list: data.list,
    totalNum: data.count
  };
}

/**
|--------------------------------------------------
| Action Handlers
|--------------------------------------------------
*/
const ACTION_HANDLERS = {
  [GET_COMMENTLIST]: (state, action) => {
    // let { list } = state;
    // list = list.concat(action.list);
    return { ...state, list: action.list, totalNum: action.totalNum };
  }
};

/**
|--------------------------------------------------
| Reducer
|--------------------------------------------------
*/
export const initialState = {
  list: [],
  pageNum: 0,
  pageSize: 10,
  totalNum: 0,
  loading: false
};

export default function(state = initialState, action) {
  const handler = ACTION_HANDLERS[action.type];
  return handler ? handler(state, action) : state;
}
